package com.maddox.relax.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by xuqingli on 2017/11/15.
 * 通用数据适配器 Listview gridview
 */

public abstract class CommonAdapter<T> extends BaseAdapter {
    /**
     * 数据源
     */
    private List<T> datas;
    private Context mContext;
    private int layoutId;//布局id

    public CommonAdapter(Context mContext, List<T> datas, int layoutId) {
        this.datas = datas;
        this.mContext = mContext;
        this.layoutId = layoutId;
        if (this.datas == null) {
            this.datas = new ArrayList<>();
        }
    }

    @Override
    public int getCount() {
        return datas.size();
    }

    @Override
    public T getItem(int position) {
        return datas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public List<T> getDatas() {
        return datas;
    }

    /**
     * 添加一条数据
     *
     * @param t 数据
     */
    public void addOneData(T t) {
        datas.add(t);
        notifyDataSetChanged();
    }

    public void addAll(List<T> das) {
        if (das!=null){
            datas.addAll(das);
            notifyDataSetChanged();
        }
    }

    /**
     * 添加数据到指定位置
     *
     * @param t     数据
     * @param index 位置下标
     */
    public void addOneData(T t, int index) {
        datas.add(index, t);
        notifyDataSetChanged();
    }

    public void clearData(){
        datas.clear();
        notifyDataSetChanged();
    }
    /**
     *  判断已有数据中是否包含t
     * @param t
     * @return
     */

    public boolean contains(T t){
        return datas.contains(t);
    }

    /**
     * 移除一条数据
     *
     * @param t
     */
    public void removeOneData(T t) {
        datas.remove(t);
        notifyDataSetChanged();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        //给每个条目绑定ViewHolder
        ViewHolder viewHolder = ViewHolder.bind(mContext, convertView, parent, layoutId, position);
        //把绑定结果反馈给调用适配器的人
        bindView(viewHolder, getItem(position),position);
        return viewHolder.getItemView();
    }

    /**
     * 把ViewHolder和对应的数据示例抽象给使用者去实现具体操作
     *
     * @param holder
     * @param t      数据
     */
    public abstract void bindView(ViewHolder holder, T t,int position);


    public static class ViewHolder {
        private Context context;
        private SparseArray<View> mViews;//缓存每个item布局中的控件
        private View itemView;//记录一条item布局
        private int position;//记录当前item的位置

        private ViewHolder(Context context, ViewGroup parent, int layoutId) {
            //初始化
            this.context = context;
            this.mViews = new SparseArray<>();
            View convertView = LayoutInflater.from(context).inflate(layoutId, parent, false);
            convertView.setTag(this);
            this.itemView = convertView;

        }

        /**
         * 绑定当前itemView与ViewHolder
         *
         * @param context
         * @param convertView 条目布局
         * @param parent      条目view的ViewGroup
         * @param layoutId    布局资源id
         * @param position    布局位置
         * @return
         */
        public static ViewHolder bind(Context context, View convertView, ViewGroup parent, int layoutId, int position) {
            ViewHolder holder;
            if (convertView == null) {
                holder = new ViewHolder(context, parent, layoutId);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            holder.position = position;

            return holder;
        }

        /**
         * 根据控件的id获取该控件
         *
         * @param viewId
         * @param <T>
         * @return
         */
        public <T extends View> T getView(int viewId) {
            T t = (T) mViews.get(viewId);
            if (t == null) {
                t = itemView.findViewById(viewId);
                mViews.put(viewId, t);
            }
            return t;
        }

        /**
         * 获取当前布局
         *
         * @return
         */
        public View getItemView() {

            return itemView;
        }

        /**
         * 获取当前布局位置
         *
         * @return
         */
        public int getPosition() {
            return position;
        }

        /**
         * 给指定id的控件设置文本
         *
         * @param tvId    控件id
         * @param content 文本内容
         * @return
         */
        public ViewHolder setText(int tvId, CharSequence content) {
            View tvView = getView(tvId);

            if (tvView instanceof TextView) {

                ((TextView) tvView).setText(content);
            }

            return this;

        }

        public ViewHolder setTextColor(int tvId, int color) {
            View tvView = getView(tvId);

            if (tvView instanceof TextView) {

                ((TextView) tvView).setTextColor(color);
            }

            return this;

        }

        /**
         * 给指定id的控件设置图片或者背景
         *
         * @param ivId     控件id
         * @param imgResid 图片资源id
         * @return
         */
        public ViewHolder setImageResource(int ivId, int imgResid) {
            View imageView = getView(ivId);
            if (imageView instanceof ImageView) {
                ((ImageView) imageView).setImageResource(imgResid);
            } else {
                imageView.setBackgroundResource(imgResid);
            }
            return this;

        }

        /**
         * 给指定id的控件设置图片或者背景
         *
         * @param ivId   控件id
         * @param bitmap bitmap图片
         * @return
         */
        public ViewHolder setImageBitmap(int ivId, Bitmap bitmap) {
            View imageView = getView(ivId);
            if (imageView instanceof ImageView) {
                ((ImageView) imageView).setImageBitmap(bitmap);
            }
            return this;

        }

        /**
         * 给指定id的控件设置点击事件
         *
         * @param viewId        控件id
         * @param clickListener 监听器
         * @return
         */
        public ViewHolder setOnClickListener(int viewId, View.OnClickListener clickListener) {

            View view = getView(viewId);
            view.setOnClickListener(clickListener);
            return this;
        }

        /**
         * 指定id控件设置是否可见
         *
         * @param viewId     控件 id
         * @param visibility 可见或者隐藏
         * @return
         */
        public ViewHolder setVisibility(int viewId, int visibility) {
            View view = getView(viewId);
            view.setVisibility(visibility);
            return this;

        }

        /**
         * 指定id控件设置tag标签
         *
         * @param viewId 控件id
         * @param obj    标签
         * @return
         */
        public ViewHolder setTag(int viewId, Object obj) {
            getView(viewId).setTag(obj);

            return this;
        }


    }
}
